package mindmap.operation;

import mindmap.components.node.node;
import mindmap.controller.layoutController;

import java.util.Collections;



/**
 * 双边布局
 */
public class Bilateral {
    private node root;

    public node getRoot() {
        return root;
    }

    public void setRoot(node root) {
        this.root = root;
    }

    public Bilateral(node root) {
        this.root = root;
    }
    public void change(){
        double x = root.getLayoutX();
        if(root.arrangement.equals("bilateral")){
            return;
        }
        int t = (root.getChildrenNode().size())/2 + (root.getChildrenNode().size()%2);
        int c = 0;
        if(root.arrangement.equals("left")){
            root.arrangement = "bilateral";
            if(root.getChildrenNode().size() != 0) {
                Collections.sort(root.getChildrenNode(),(o1,o2)->{
                    return (int)o1.getLayoutY() - (int)o2.getLayoutY();
                });
                for(node n : root.getChildrenNode()){
                    if(c < t){
                        node.leftChildrenNode.add(n);
                    }
                    if(c >= t){
                        node.rightChildrenNode.add(n);
                        n.arrangement = "right";
                        if(c == t){
                            n.PreNode.SufNode = null;
                            n.PreNode = null;
                        }
                        n.getLine().startXProperty().unbind();
                        n.getLine().startXProperty().bind(n.getParentNode().layoutXProperty().add(n.getRectangle().getWidth()));
                        n.getLine().endXProperty().unbind();
                        n.getLine().endXProperty().bind(n.layoutXProperty());
                        n.setLayoutX(2*x - n.getLayoutX());
                        node.deleteList.clear();
                        n.sumDeleteNode(n);
                        for(node n1 : node.deleteList){
                            n1.arrangement = "right";
                            n1.getLine().startXProperty().unbind();
                            n1.getLine().startXProperty().bind(n1.getParentNode().layoutXProperty().add(n1.getRectangle().getWidth()));
                            n1.getLine().endXProperty().unbind();
                            n1.getLine().endXProperty().bind(n1.layoutXProperty());
                            n1.setLayoutX(2*x - n1.getLayoutX());
                        }
                    }
                    c++;
                }

            }
        } else {
            root.arrangement = "bilateral";
            if(root.getChildrenNode().size() != 0) {
                Collections.sort(root.getChildrenNode(),(o1,o2)->{
                    return (int)o1.getLayoutY() - (int)o2.getLayoutY();
                });
                for(node n : root.getChildrenNode()){
                    if(c < t){
                        node.rightChildrenNode.add(n);
                    }
                    if(c >= t){
                        node.leftChildrenNode.add(n);
                        n.arrangement = "left";
                        if(c == t){
                            n.PreNode.SufNode = null;
                            n.PreNode = null;
                        }
                        n.getLine().startXProperty().unbind();
                        n.getLine().startXProperty().bind(n.getParentNode().layoutXProperty());
                        n.getLine().endXProperty().unbind();
                        n.getLine().endXProperty().bind(n.layoutXProperty().add(n.getRectangle().getWidth()));
                        n.setLayoutX(2*x - n.getLayoutX());
                        node.deleteList.clear();
                        n.sumDeleteNode(n);
                        for(node n1 : node.deleteList){
                            n1.arrangement = "left";
                            n1.getLine().startXProperty().unbind();
                            n1.getLine().startXProperty().bind(n1.getParentNode().layoutXProperty());
                            n1.getLine().endXProperty().unbind();
                            n1.getLine().endXProperty().bind(n1.layoutXProperty().add(n1.getRectangle().getWidth()));
                            n1.setLayoutX(2*x - n1.getLayoutX());
                        }
                    }
                    c++;
                }

            }
        }
        root.ConfirmBoth(root);
        double rh = (node.rightMinHeight.getLayoutY() + node.rightMaxHeight.getLayoutY())/2;
        double lh = (node.leftMaxHeight.getLayoutY() + node.leftMinHeight.getLayoutY())/2;
        rh = root.getLayoutY() - rh;
        lh = root.getLayoutY() - lh;
        node.deleteList.clear();
        root.sumDeleteNode(root);
        for(node n : node.deleteList){
            if(n.arrangement.equals("right")){
                n.setLayoutY(n.getLayoutY() + rh);
            }
            if(n.arrangement.equals("left")){
                n.setLayoutY(n.getLayoutY() + lh);
            }
        }
        layoutController.getInstance().hideSelectRect();
        layoutController.getInstance().selectNode = null;
    }
}
